package com.example.workapp;

import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.webkit.ConsoleMessage;
import android.webkit.JavascriptInterface;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;

import androidx.appcompat.app.AppCompatActivity;

public class screen_web_activity extends AppCompatActivity {

//    @Override
//    protected void onCreate(Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//        EdgeToEdge.enable(this);
//        setContentView(R.layout.activity_screen_web);
//        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
//            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
//            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
//            return insets;
//        });
//    }
private WebView mWebView;
    private  final int hand_js_doJsFunction = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_screen_web);

        IniteWebView();
    }
    //绑定webview配置及加载本地页面
    private void IniteWebView(){
        mWebView = (WebView) findViewById(R.id.screen_webView);
        mWebView.setWebChromeClient(new WebChromeClient() {
            @Override
            public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
                // 打印控制台输出
                // 你可以根据需要将消息发送到 Logcat，或者显示在 UI 上
                String message = consoleMessage.message() + " -- From line " + consoleMessage.lineNumber();
                System.out.println(message); // 或者使用 Log.d
                return super.onConsoleMessage(consoleMessage);
            }
        });
        WebSettings settings = mWebView.getSettings();
        //允许js交互
        settings.setJavaScriptEnabled(true);
        settings.setAllowFileAccess(true);
        settings.setAllowContentAccess(true);
        settings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        settings.setBlockNetworkLoads(false);

        Intent intent=getIntent();
        String target_ip = intent.getStringExtra("recv_ip");

        // 获取传递的 IP 地址
        if (target_ip != null) {
            mWebView.loadUrl("http://" + target_ip + ":9999");
        }

        // 允许 JavaScript 调用 Java 方法
        mWebView.addJavascriptInterface(new JsBridge(), "AndroidJS");
        executeJs(
//                "window.onload = function(){\n" +
                "     console.log(\"aaaaasasasas\")"+
                "    // 删除所有 <br>、<button> 和 <input> 标签\n" +
                "    document.querySelectorAll(\"br, button, input\").forEach(el => {console.log(\"el-target\", el);el.remove();});\n" +
                "\n" +
                "    // 给所有 <img> 标签添加内联样式 margin: 0\n" +
                "    document.querySelectorAll(\"img\").forEach(img => img.style.margin = \"0\");\n" +
//                "};\n"+
                "console.log(\"1234567890\")"
        );

    }

    // JavaScript 交互接口
    public class JsBridge {
        @JavascriptInterface
        public void GetAppInfo() {
            String appInfo = getAppInfo();
            runOnUiThread(() -> executeJs("backInfo('" + appInfo + "')"));
        }
    }

    // 执行 JavaScript 代码
    private void executeJs(String jsCode) {
        if (mWebView != null) {
            mWebView.evaluateJavascript("javascript:" + jsCode, null);
        }
    }

    // 获取应用信息
    private String getAppInfo() {
        try {
            PackageManager packageManager = getPackageManager();
            PackageInfo packageInfo = packageManager.getPackageInfo(getPackageName(), 0);
            return "版本号：" + packageInfo.versionName + "\n包名：" + packageInfo.packageName;
        } catch (Exception e) {
            e.printStackTrace();
            return "获取应用信息失败";
        }
    }
}